package com.dormitory.management.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dormitory.management.system.entity.Access;
import com.dormitory.management.system.entity.Housemaster;
import com.dormitory.management.system.entity.Student;
import com.dormitory.management.system.enums.UserEnum;
import com.dormitory.management.system.mapper.HousemasterMapper;
import com.dormitory.management.system.resp.AccessResp;
import com.dormitory.management.system.mapper.AccessMapper;
import com.dormitory.management.system.req.AccessPageReq;
import com.dormitory.management.system.service.AccessService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dormitory.management.system.service.UserService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 门禁表 服务实现类
 * </p>
 *
 * @author
 * @since 2021-12-17
 */
@Service
public class AccessServiceImpl extends ServiceImpl<AccessMapper, Access> implements AccessService {
    @Autowired
    UserService userService;
    @Autowired
    HousemasterMapper housemasterMapper;

    @Override
    public IPage<AccessResp> getPage(AccessPageReq req) {
        Page<AccessResp> page = new Page<>(req.getPageCurrent(), req.getPageSize());
        QueryWrapper<AccessResp> query = new QueryWrapper<>();
        if (userService.getRoleKey(req.getUsername()).equals(UserEnum.USER_HOUSEMASTER.getRoleKey())) {
            /*Housemaster housemaster = housemasterMapper.selectOne(new QueryWrapper<Housemaster>().lambda().eq(Housemaster::getIdentityCard,req.getUsername()));*/
            query.eq("housemaster.identity_card",req.getUsername())
                    .like(req.getRoom() != 0, "room.room", req.getRoom())
                    .like(req.getStuId() != null, "student.stu_id", req.getStuId())
                    .like(req.getDevice() != null, "access.device", req.getDevice())
                    .like(req.getName() != null, "student.name", req.getName())
                    .eq("student.status_id", 0)
                    .eq("housemaster.status_id",0)
                    .apply(req.getGmtModifiedEarliest()!=null,"TO_DAYS(access.swiping_time) >= TO_DAYS('"+ DateFormatUtils.format(req.getGmtModifiedEarliest(), "yyyy-MM-dd HH:mm:ss")+"')")
                    .apply(req.getGmtModifiedLatest()!=null,"TO_DAYS(access.swiping_time) <= TO_DAYS('"+ DateFormatUtils.format(req.getGmtModifiedLatest(), "yyyy-MM-dd HH:mm:ss")+"')");
//                    .ge(req.getGmtModifiedEarliest() != null, "access.swiping_time", req.getGmtModifiedEarliest())
//                    .le(req.getGmtModifiedLatest() != null, "access.swiping_time", req.getGmtModifiedLatest());
            return this.baseMapper.getAccessPageHousemaster(page, query);
        } else if (userService.getRoleKey(req.getUsername()).equals(UserEnum.USER_STUDENT.getRoleKey())) {
            query.eq("student.stu_id",req.getUsername())
                    .like(req.getRoom() != 0, "room.room", req.getRoom())
                    .like(req.getStuId() != null, "student.stu_id", req.getStuId())
                    .like(req.getDevice() != null, "access.device", req.getDevice())
                    .like(req.getName() != null, "student.name", req.getName())
                    .eq("student.status_id", 0)
                    .apply(req.getGmtModifiedEarliest()!=null,"TO_DAYS(access.swiping_time) >= TO_DAYS('"+ DateFormatUtils.format(req.getGmtModifiedEarliest(), "yyyy-MM-dd HH:mm:ss")+"')")
                    .apply(req.getGmtModifiedLatest()!=null,"TO_DAYS(access.swiping_time) <= TO_DAYS('"+ DateFormatUtils.format(req.getGmtModifiedLatest(), "yyyy-MM-dd HH:mm:ss")+"')");
//                    .ge(req.getGmtModifiedEarliest() != null, "access.swiping_time", req.getGmtModifiedEarliest())
//                    .le(req.getGmtModifiedLatest() != null, "access.swiping_time", req.getGmtModifiedLatest());
            return this.baseMapper.getAccessPageStudent(page, query);
        } else if (userService.getRoleKey(req.getUsername()).equals(UserEnum.USER_ADMIN.getRoleKey())) {
            query.like(req.getRoom() != 0, "room.room", req.getRoom())
                    .like(req.getStuId() != null, "student.stu_id", req.getStuId())
                    .like(req.getDevice() != null, "access.device", req.getDevice())
                    .like(req.getName() != null, "student.name", req.getName())
                    .eq("student.status_id", 0)
                    .apply(req.getGmtModifiedEarliest()!=null,"TO_DAYS(access.swiping_time) >= TO_DAYS('"+ DateFormatUtils.format(req.getGmtModifiedEarliest(), "yyyy-MM-dd HH:mm:ss")+"')")
                    .apply(req.getGmtModifiedLatest()!=null,"TO_DAYS(access.swiping_time) <= TO_DAYS('"+ DateFormatUtils.format(req.getGmtModifiedLatest(), "yyyy-MM-dd HH:mm:ss")+"')");
//                    .ge(req.getGmtModifiedEarliest() != null, "access.swiping_time", req.getGmtModifiedEarliest())
//                    .le(req.getGmtModifiedLatest() != null, "access.swiping_time", req.getGmtModifiedLatest());
            return this.baseMapper.getAccessPageUser(page,query);
        }
        return null;
    }
}